#!/usr/bin/env perl
#
# SimiCon web server standalone script
#
# http://abagyan.ucsd.edu/SimiCon/
#
# Author: Manuel Rueda Ph.D.
# University of California, San Diego
# Skaggs School of Pharmacy & Pharmaceutical Sciences
# 9500 Gilman Drive, MC 0747
# La Jolla, CA  92093-0747
# manuelrueda@ucsd.edu
#
# Latest version: 1.1
#
# Notes:
#
# This is an example "generic" script. The user must replace
# the input parameters (see below) according to the naming of
# the files to compare.
#
# The script works either with "PDB IDs" or "PDB text files"
# To work with "PDB IDs" see input "Example A"
# To work with "PDB text files" see input "Example B"
#
# For multiple SimiCon CGI executions we recommend these two options:
#
# i/  Use a loop from an external Linux shell script
#     (e.g., 'foreach' in TCSH, or 'for' in BASH).
#     The values of 'proteinTarPDB' or 'proteinTarFile' must
#     be replaced. You can use 'sed' from outside,
#     or you may enter them as a Perl arguments (e.g., $ARGV[0], etc)
#
# ii/ Use a for loop inside this script which consists of
#     the names for the 'target' files to be compared
#
# PS: Please, do not abuse the web server
#     Run your calculations sequentially if possible. Thanks!
#     Do not hesitate to contact the author in case you get any error

use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);

my $ua  = new LWP::UserAgent;
my $url = 'http://abagyan.ucsd.edu/SimiCon/';

# START HERE FOR LOOP WITH TARGET LIST <- OPTIONAL USER MODIFICATION

# Send form to the SimiCon Home page
# Example A: using PDB IDs (currently used)
my $req = POST $url,
  Content_Type => 'form-data',
  Content      => [
               proteinRefPDB => '2f9q',    #<- USER MODIFICATION
               proteinTarPDB => '2f9q',    #<- USER MODIFICATION
               chainRefPDB   => 'a',       #<- USER MODIFICATION
               chainTarPDB   => 'b',       #<- USER MODIFICATION
               ligandRefPDB  => 'hem',     #<- USER MODIFICATION
               ligandTarPDB  => 'hem',     #<- USER MODIFICATION
               cutoff        => '4.0'      #<- USER MODIFICATION
  ];

# Example B: Using PDB text files
#my $req = POST $url,
Content_Type => 'form-data',
  Content    => [
               pageAction     => 'upload',
               newCode        => '23',
               MAX_FILE_SIZE  => '6000000',
               proteinRefFile => ['3eml.ent'],       #<- USER MODIFICATION
               proteinTarFile => ['3400_1.pdb'],     #<- USER MODIFICATION
               ligandRefFile  => ['3eml_1.ent'],     #<- USER MODIFICATION
               ligandTarFile  => ['3400_11.pdb'],    #<- USER MODIFICATION
               cutoff         => '4.0'               #<- USER MODIFICATION
  ];

die "Couldn't get $url" unless defined $req;

##############################################################
# DO NOT TOUCH CODE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING #
##############################################################

# Retrieve the content as a string
my $SimiCon_Home = $ua->request($req)->as_string;

# Parse results to catch the final page
my @RESULTS_PAGE_ONE_LIST = split /\n/, $SimiCon_Home;
my $link_page_two;
foreach (@RESULTS_PAGE_ONE_LIST) {
    $link_page_two = $_ if /http:\/\/abagyan.ucsd.edu\/SimiCon/;
}

$link_page_two =~ m/'(\S+)'/;
$link_page_two = $1;
die "Couldn't get the JobId URL" unless defined $link_page_two;

# Now we retrieve the actual results from last page
my $req2 = POST $link_page_two, [];
die "Couldn't get $link_page_two" unless defined $req2;

# And we parse the results from final page HTML
my $SimiCon_results = $ua->request($req2)->as_string;
my @SIMICON_RESULTS_LIST = split /\n/, $SimiCon_results;
my ( $job_id, $results_summary );
foreach (@SIMICON_RESULTS_LIST) {
    $job_id = $_ if /SimiCon Id Job/;
    $results_summary = $results_summary . $_ . "\n" if /Reference has|Equivalent contacts|Coverage =/;
}
$job_id =~ m/SimiCon Id Job: (\d+) Results/;
$job_id = $1;
print "Id Job: $job_id\n";
$results_summary =~ s/&cap;|&nbsp;|<b>|<\/b>|<br \/>//g;
die "Something went wrong, please check the nomenclature of your files\n" unless ( defined $job_id and defined $results_summary );
print $results_summary;

# Example: if we want to print the table
#my $csv_table = $link_page_two;
#$csv_table =~s/html//;
#$csv_table = $csv_table . "$job_id.csv";
#my $req3 =  POST $csv_table, [];
#my $csv_table_print = $ua->request($req3)->as_string;
#print "$csv_table_print";

##################################################################
# DO NOT TOUCH the UPPER CODE UNLESS YOU KNOW WHAT YOU ARE DOING #
##################################################################

# END HERE FOR LOOP WITH TARGET LIST <- OPTIONAL USER MODIFICATION
